<?php

/**
 * 网易H5服务
 * @desc
 */
class Api_NTES extends PhalApi_Api {
    public function getRules() {
        return [
            'createImgs' => [
                "key1"   => ['name' => 'key1', 'type' => 'string', 'require' => true, 'desc' => '关键词1'],
                "key2"   => ['name' => 'key2', 'type' => 'string', 'require' => true, 'desc' => '关键词2'],
                "desc"   => ['name' => 'desc', 'type' => 'string', 'require' => true, 'desc' => '故事叙述'],
                "picNUM" => ['name' => 'pic_num', 'type' => 'int', 'min' => 0, 'max' => 8, 'require' => true, 'desc' => '素材编号'],

            ],
            'upload'=>[
                'file' => [
                    'name' => 'file',
                    'type' => 'file',
                    'require' => true,
                    'min' => 0,
                    'max' => 1024 * 1024,
                    'range' => ['image/jpg', 'image/jpeg', 'image/png'],
                    'ext' => ['jpg', 'jpeg', 'png']
                ],
            ]
        ];
    }

    /**
     * 图片上传
     * @desc 必须POST form属性需要加enctype="multipart/form-data"
     * @return uri imgurl 图片地址
     * @throws PhalApi_Exception
     */
    public function upload(){
        $filename = DI()->helper->createRandStr(10);
        DI()->UCloud->set('save_path','ntes/'.date('Y/m/d'));
        DI()->UCloud->set('file_name', $filename);
        $res = DI()->UCloud->upfile($this->file);
        if (!$res) {
            throw new PhalApi_Exception("上传图片失败", 1001);
        };
        return [
            'imgurl'=>'http://images.menma.me' . $res['url'],
        ];
    }
    /**
     * 图片合成 todo
     * @desc 功能未完成
     * @return string imgurl 图片url
     */
    public function createImgs() {

        $filename = DI()->helper->createRandStr(10);
        $tempImg = API_ROOT . '/Runtime/' . $filename . '.jpg';
        $cdnURI = '/NTES/' . $filename . '.jpg';
        return $this->file;
        return [
            'imgurl'=>'http://api.menma.me/Runtime/images/1.jpg'
        ];
//        $loc=[
//            [
//                API_ROOT . '/NTES/fonts/minglan.ttf',
//                [416/2,126],
//                '#b0292d',
//                18
//            ]
//        ];
//
//        list($ttf,$loc,$color,$fontSize)=$loc[0];
//
//        $flen=DI()->helper->abslength($this->key1);
//
//        $loc[0]=$loc[0]-($fontSize*$flen)/2;
//
//        $PhalApi_Image = new Image_Lite();
//        $PhalApi_Image
//            ->open(API_ROOT . '/NTES/images/1.jpg')
//            ->text($this->key1,$ttf ,$fontSize, $color, $loc, 0, 0)
//            ->save($tempImg);
//
//
//
//        $opts = [
//            UpYun::CONTENT_MD5 => md5(file_get_contents($tempImg))
//        ];
//        $fh = fopen($tempImg, 'rb');
//        $rsp = DI()->Upyun->writeFile($cdnURI, $fh, True, $opts);   //
//        fclose($fh);
//        if (!$rsp) {
//            throw new PhalApi_Exception("生成图片失败", 1001);
//        };
//        @unlink($tempImg);
//        return [
//            'imgurl'=>'http://images.menma.me' . $cdnURI,
//        ];
    }

//    function face(){
//        $facepp=new Facepp_Lite();
//        $params['url']          = 'http://www.faceplusplus.com.cn/wp-content/themes/faceplusplus/assets/img/demo/1.jpg';
//        $response               = $facepp->execute('/detection/detect',$params);
//        return @json_decode($response['body']);
//    }

}